package com.kangkang.reflection.classEx;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 描述:
 * 日期: 2022-03-18 <br>
 *
 * @author: 旺旺老师<br>
 * @version: 1.0<br>
 */
public class ClassDemo {
    public static void getClass2() {
        //1 Object提供了getClass()方法返回Class对象
        Student student = new Student();
        Class<? extends Student> cls1 = student.getClass();
        System.out.println(cls1);

        Class<?> cls2 = null;
        try {
            //2 Class.forName静态方法  类结构加载到内存
            cls2 = Class.forName("com.wangwang.classEx.Student");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println(cls2);

        //3 类名.class 返回Class对象
        Class<Student> cls3 = Student.class;
        System.out.println(cls3);
        printPercent(cls3);
    }
    public static void printPercent(Class cls) {
        //1 得到父类
        Class superCls = cls.getSuperclass();
        System.out.println("superClass : " + superCls.getName());

        //2 得到接口
        Class[] supserInterfaces = cls.getInterfaces();
        if (supserInterfaces.length != 0 ) {
            System.out.println(cls.getName() + "实现的接口有:");
            for (Class superInterface : supserInterfaces) {
                System.out.println(superInterface);
            }
        } else {
            System.out.println(cls.getName() + "没有实现接口");
        }
    }
    public static void main(String[] args) {
        //getClass2();
        // getMethods();
       getFilds();


    }
    public static void getFilds() {
        Class<Student> cls = Student.class;
        //cls.getFields();
        //cls.getField("")
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field.getModifiers() + " " + field.getName() + " " + field.getType());
        }
        
    }
    public static void getMethods() {
        Class<Student> cls = Student.class;
        //getMethods(); 得到所有 包括父类
        //getDeclaredMethods(); 只是本类中定义的
        Method[] ms = cls.getDeclaredMethods();
        for (Method method : ms) {
            System.out.println(method.getName() + " "
                    + method.getModifiers() + " "
                    + method.getReturnType() + " "
                    + Arrays.toString(method.getParameterTypes()));
        }

        //cls.getMethod();
    }
}




